package com.cjxt.ams.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjxt.ams.dao.DeptDao;
import com.cjxt.ams.domain.Dept;
import com.cjxt.ams.domain.TreeNode;
import com.cjxt.ams.service.DeptService;
import com.cjxt.ams.utils.R;
import com.cjxt.ams.utils.TreeNodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;



/**
 * @author fangzhiyu
 * @version V1.0
 * @Description: 部门管理控制类
 * @className: DeptController
 * @date 2021/11/23 11:15
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */

@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController extends AbstractController {

    @Resource
    DeptService deptService;

    @Resource
    DeptDao deptDao;


     /**
      * @description: 新增部门
      * @author fangzhiyu
      * @date 2021/12/9 16:53
      * @param dept	部门实例
      * @return com.cjxt.ams.util.R
     */

    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8")
    public R register(@RequestBody Dept dept) {
        dept.setCreateBy(getUser().getUsername());
        return deptService.insertDept(dept);
    }

    /**
     * @description: 根据部门ID删除部门
     * @author fangzhiyu
     * @date 2021/12/9 16:55
     * @param deptId 部门id
     * @return com.cjxt.ams.util.R
     */

    @RequestMapping("/delete")
    public R delete(Integer deptId){
        //判断是否有子部门
        List<Integer> deptList = deptService.queryDeptIdList(deptId);
        if(deptList.size() > 0){
            return R.error("请先删除子部门");
        }
        deptService.removeById(deptId);
        return R.ok();
    }

    /**
     * @description: 修改部门信息
     * @author fangzhiyu
     * @date 2021/12/9 16:55
     * @param dept	部门实例
     * @return com.cjxt.ams.util.R
     */

    @RequestMapping(value = "/alter",produces = "application/json;charset=utf-8")
    public R alter(@RequestBody Dept dept) {
        dept.setUpdateBy(getUser().getUsername());
        return deptService.updateDept(dept);

    }


    /**
     * @description: 查询所有部门信息
     * @author fangzhiyu
     * @date 2021/12/9 16:54
     * @return com.cjxt.ams.util.R
     */

    @GetMapping("/findAll")
    public R findAll() {
        List<Dept> dept = deptService.queryDeptList(deptService.list());
        return R.ok().put("data",dept);
    }


    /**
     * @description: 查询所有部门信息
     * @author fangzhiyu
     * @date 2021/12/9 16:54
     * @return java.util.List<com.cjxt.ams.domain.dept>
     */
    @GetMapping("/findAll01")
    public List<Dept> findAll01(){
        List<Dept> dept01 = deptDao.selectList(null);
        return dept01;
    }

    /**
     * @description: 返回前台加载部门树需要的数据结构
     * @author fangzhiyu
     * @date 2021/12/9 17:15
     * @return java.util.List<com.cjxt.ams.domain.TreeNode>
     */

    @RequestMapping("/JsonTree")
    public List<TreeNode> JsonTree(){

        List<TreeNode> list = deptService.treeData();
        List<TreeNode> list1 = TreeNodeUtil.transformData(list, 0);
        return list1;
    }

}

